/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ortustve.onlinescrapper;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import sagex.api.MediaFileAPI;

/**
 *
 * @author jphipps
 */
public class MediaObject extends ortustve.ortusvars {

        private Object mo;
	private String name = null;
        private String showtitle = null;
        private String episodetitle = null;
        private boolean metadata_exist = false;
        private boolean media_exist = false;
        private int metadata = 0;
        private int fanart = 0;
        private int mediatype = 0;
        private int mediagroup = 1;
        private String metadatasource = "";
        private int scantype = 0;
	private boolean likely_tv = false;

	private String imdbid = null;
	private String tmdbid = null;
	private String tvdbid = null;

        int MEDIATYPE_none = 0;
        int MEDIATYPE_recording = 1;
        int MEDIATYPE_movie = 2;
        int MEDIATYPE_series = 3;

        int MEDIAGROUP_recorded = 0;
        int MEDIAGROUP_imported = 1;

        public MediaObject(Object o) {
            mo = o;
            if ( MediaFileAPI.IsTVFile(mo)) {
                mediatype = MEDIATYPE_recording;
                mediagroup = MEDIAGROUP_recorded;
            }

        }

        public MediaObject(Object o, int metadata, int fanart) {
            mo = o;
            this.metadata = metadata;
            this.fanart = fanart;

            if ( MediaFileAPI.IsTVFile(mo)) {
                mediatype = MEDIATYPE_recording;
                mediagroup = MEDIAGROUP_recorded;
            }
        }

        public Object GetMedia() {
            return mo;
        }

	public void SetLikelyTV() {
		likely_tv = true;
	}
	public boolean IsLikelyTV() {
		return likely_tv;
	}

        public void SetMediaTypeRecording() {
            mediatype = MEDIATYPE_recording;
        }

        public boolean IsMediaTypeRecording() {
            if ( mediatype == MEDIATYPE_recording )
                return true;
            else
                return false;
        }

        public void SetMediaTypeSeries() {
            mediatype = MEDIATYPE_series;
        }

        public boolean IsMediaTypeSeries() {
            if ( mediatype == MEDIATYPE_series )
                return true;
            else
                return false;
        }

        public void SetMediaTypeMovie() {
            mediatype = MEDIATYPE_recording;
        }

        public boolean IsMediaTypeMovie() {
            if ( mediatype == MEDIATYPE_movie )
                return true;
            else
                return false;
        }

        public void SetMediaGroupImported() {
            mediagroup = MEDIAGROUP_imported;
        }

        public void SetMediaGroupRecorded() {
            mediagroup = MEDIAGROUP_recorded;
        }

        public boolean IsMediaGroupImported() {
            if ( mediagroup == MEDIAGROUP_recorded)
                return false;
            else
                return true;
        }

        public boolean IsMediaGroupRecorded() {
            if ( mediagroup == MEDIAGROUP_recorded)
                return true;
            else
                return false;
        }

        public int GetScanType() {
            return scantype;
        }
        public void SetScanType(int scantype) {
            this.scantype = scantype;
        }
        public int GetMediaGroup() {
            return mediagroup;
        }

	public String GetName() {
		return name;
	}

	public void SetName(String name) {
		this.name = name;
	}
	
        public void SetShowTitle(String showtitle) {
            this.showtitle = showtitle;
        }

        public String GetShowTitle() {
            return showtitle;
        }

        public void SetEpisodeTitle(String episodetitle) {
            this.episodetitle = episodetitle;
        }

        public String GetEpisodeTitle() {
            return episodetitle;
        }

        public void SetMetadataExist() {
            metadata_exist = true;
        }

        public boolean IsMetadataExist() {
            return metadata_exist;
        }

        public void SetMediaExist() {
            media_exist = true;
        }

        public boolean IsMediaExist() {
            return media_exist;
        }

        public void SetMetadataFlag(int metadata) {
            this.metadata = metadata;
        }

        public boolean IsMetadataNone() {
            if ( metadata == ortustve.onlinescrapper.api.METADATA_none)
                return true;
            else
                return false;
        }
        public boolean IsMetadataUpdate() {
            if ( metadata == ortustve.onlinescrapper.api.METADATA_update)
                return true;
            else
                return false;
        }
        public boolean IsMetadataFull() {
            if ( metadata == ortustve.onlinescrapper.api.METADATA_full)
                return true;
            else
                return false;
        }

        public void SetFanartFlag(int fanart) {
            this.fanart = fanart;
        }

        public boolean IsFanartNone() {
            if ( fanart == ortustve.onlinescrapper.api.FANART_none)
                return true;
            else
                return false;
        }
        public boolean IsFanartUpdate() {
            if ( fanart == ortustve.onlinescrapper.api.FANART_update)
                return true;
            else
                return false;
        }
        public boolean IsFanartFull() {
            if ( fanart == ortustve.onlinescrapper.api.FANART_full)
                return true;
            else
                return false;
        }

        public boolean IsFanartDownload() {
            if (fanart == ortustve.onlinescrapper.api.FANART_update || fanart == ortustve.onlinescrapper.api.FANART_full )
                return true;
            else
                return false;
        }
        
        public boolean IsProcessRecording() {
            if ( IsMediaTypeRecording() &&
                 ( IsMetadataFull() || ! IsMediaExist() || IsFanartFull()) )
                return true;
            else
                return false;
        }

        public String GetMetadataSource() {
            return metadatasource;
        }
        public void SetMetadataSource(String metadatasource) {
            this.metadatasource = metadatasource;
        }

	public String GetIMDBId() {
		return imdbid;
	}
	public void SetIMDBId(String imdbid) {
		this.imdbid = imdbid;
	}
	public String GetTVDBId() {
		return tvdbid;
	}
	public void SetTVDBId(String tvdbid) {
		this.tvdbid = tvdbid;
	}
	public String GetTMDBId() {
		return tmdbid;
	}
	public void SetTMDBId(String tmdbid) {
		this.tmdbid = tmdbid;
	}

    public boolean ReadProperty() {
        Properties mp = new Properties();
        try {
            File propfile = new File(MediaFileAPI.GetFileForSegment(mo, 0).getAbsolutePath() + ".properties");

            if ( propfile.exists()) {
		ortustve.api.DebugLog(TRACE2,"readProperty: " + propfile.getAbsolutePath());
                mp.load(new FileInputStream(propfile));
                String[] workproviders = mp.getProperty("MediaProviderDataID").split(";");
		for ( String prov : workproviders ) {
			String[] subprov = prov.split(":");
	                if ( subprov[0].equalsIgnoreCase("themoviedb"))
			     tmdbid = subprov[1];
			if ( subprov[0].equalsIgnoreCase("theTVDB"))
			     tvdbid = subprov[1];
	                if ( subprov[0].equalsIgnoreCase("imdb"))
	                     imdbid = subprov[1];
		}
                name = mp.getProperty("MediaTitle");
		if ( tmdbid == null && imdbid== null && tvdbid == null)
			return false;
		else
			ortustve.api.DebugLog(TRACE2,"TVDB: " + tvdbid + " TMDB: " + tmdbid + " IMDB: " + imdbid);
                return true;
            } else
                return false;
        } catch (Exception e) {
                ortustve.api.DebugLog(INFO, "ReadProperty Exception: " + e );
                return false;
        }
    }
//    public void WriteProperty(Object mediafile) {
//        SortedProperties mp = new SortedProperties();
//
//        mp.setProperty("MediaTitle", name);
//        mp.setProperty("Title", name);
//        mp.setProperty("Description", overview);
//        mp.setProperty("RunningTime", runtime);
//        if ( mediatype == 2 )
//            mp.setProperty("MediaType", "Movie");
//        else if ( mediatype == 3)
//            mp.setProperty("MediaType", "Series");
//        else if ( mediatype == 4)
//            mp.setProperty("MediaType", "HomeMovie");
//        else
//            mp.setProperty("MediaType","Recording");
//
//        mp.setProperty("MPAA",rated);
//        mp.setProperty("UserRating",String.valueOf(Math.round(rating)));
//        mp.setProperty("OriginalAirDate", releasedate);
//        mp.setProperty("Trailer", trailer);
//
//        StringBuffer xgenre = new StringBuffer(10);
//        for ( String g : genres) {
//            xgenre.append(g + " ");
//        }
//        mp.setProperty("Genre", xgenre.toString().trim());
//
//        StringBuffer xactor = new StringBuffer(10);
//        StringBuffer xdirector = new StringBuffer(10);
//        StringBuffer xwriter = new StringBuffer(10);
//        for ( CastItem ci : cast ) {
//            ortus.api.DebugLog(TRACE2,"Cast: " + ci.GetName() + " doing: " + ci.GetJob());
//            if ( ci.GetJob().equals("Actor"))
//                xactor.append(ci.GetName() + " -- " + ci.GetCharacter() + ";\n");
//            if ( ci.GetJob().equals("Director"))
//                xdirector.append(ci.GetName() + ";");
//            if ( ci.GetJob().equals("Writer"))
//                xwriter.append(ci.GetName() + ";");
//        }
//        mp.setProperty("Director", xdirector.toString().trim());
//        mp.setProperty("Writer", xwriter.toString().trim());
//        mp.setProperty("Actor", xactor.toString().trim());
//
//	String metadataprovider = "MediaProviderDataID";
//        if ( ! id.isEmpty())
//	    metadataprovider+="themoviedb:" + id;
//	if ( !id.isEmpty() && !imdbid.isEmpty())
//		metadataprovider+=";";
//        if ( ! imdbid.isEmpty())
//	    metadataprovider+="imdb:" + imdbid;
//
//        mp.setProperty("MediaProviderDataID", metadataprovider);
//
//        try {
//            mp.store(new FileOutputStream(new File(MediaFileAPI.GetFileForSegment(mediafile, 0).getAbsolutePath() + ".properties")), "Generator: Ortus; MediaFile: file:" + MediaFileAPI.GetFileForSegment(mediafile, 0).getAbsolutePath());
//        } catch (IOException ex) {
//            ortus.api.DebugLog(ERROR, "Movie: Failed to write property file: " + MediaFileAPI.GetFileForSegment(mediafile, 0).getAbsolutePath() + ".properties" );
//            return;
//        }
//
//        return;
//    }
}
